package html;
import javax.swing.JOptionPane;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.util.Vector;

public class HtmlTreeModel implements TreeModel {
    private Vector<TreeModelListener> treeModelListeners =
        new Vector<TreeModelListener>();
    private DefaultMutableTreeNode rootDefaultMutableTreeNode;

    public HtmlTreeModel(DefaultMutableTreeNode root) {
        rootDefaultMutableTreeNode = root;
    }

    /**
     * Used to toggle between show ancestors/show descendant and
     * to change the root of the tree.
     */

//////////////// Fire events //////////////////////////////////////////////

    /**
     * The only event raised by this model is TreeStructureChanged with the
     * root as path, i.e. the whole tree has changed.
     */
    protected void fireTreeStructureChanged(DefaultMutableTreeNode oldRoot) {
        int len = treeModelListeners.size();
        TreeModelEvent e = new TreeModelEvent(this, 
                                              new Object[] {oldRoot});
        for (TreeModelListener tml : treeModelListeners) {
            tml.treeStructureChanged(e);
        }
    }


//////////////// TreeModel interface implementation ///////////////////////

    /**
     * Adds a listener for the TreeModelEvent posted after the tree changes.
     */
    public void addTreeModelListener(TreeModelListener l) {
        treeModelListeners.addElement(l);
    }

    /**
     * Returns the child of parent at index index in the parent's child array.
     */
    public Object getChild(Object parent, int index) {
        DefaultMutableTreeNode p = (DefaultMutableTreeNode)parent;
        return p.getChildAt(index);
    }

    /**
     * Returns the number of children of parent.
     */
    public int getChildCount(Object parent) {
        DefaultMutableTreeNode p = (DefaultMutableTreeNode)parent;
        return p.getChildCount();
    }

    /**
     * Returns the index of child in parent.
     */
    public int getIndexOfChild(Object parent, Object child) {
        DefaultMutableTreeNode p = (DefaultMutableTreeNode)parent;
        return p.getIndex((TreeNode) child);
    }


    /**
     * Returns the root of the tree.
     */
    public Object getRoot() {
        return rootDefaultMutableTreeNode;
    }

    /**
     * Returns true if node is a leaf.
     */
    public boolean isLeaf(Object node) {
        DefaultMutableTreeNode p = (DefaultMutableTreeNode)node;

        return p.getChildCount() == 0;
    }

    /**
     * Removes a listener previously added with addTreeModelListener().
     */
    public void removeTreeModelListener(TreeModelListener l) {
        treeModelListeners.removeElement(l);
    }

    /**
     * Messaged when the user has altered the value for the item
     * identified by path to newValue.  Not used by this model.
     */
    public void valueForPathChanged(TreePath path, Object newValue) {
        System.out.println("*** valueForPathChanged : "
                           + path + " --> " + newValue);
    }
    
    
    public void setRoot(DefaultMutableTreeNode node)
    {
    	rootDefaultMutableTreeNode=node;
    	fireTreeStructureChanged(rootDefaultMutableTreeNode);
    	 JOptionPane.showMessageDialog(null,"Update tree");
    	
    }
}